package com.itlaobing.demo.service.impl;

import com.itlaobing.demo.dao.UserinfoDao;
import com.itlaobing.demo.dao.impl.UserinfoDaoImpl;
import com.itlaobing.demo.ex.ServiceException;
import com.itlaobing.demo.model.UserinfoModel;
import com.itlaobing.demo.service.UserinfoService;

import javax.servlet.http.HttpServletRequest;

/**
 * @Classname UserinfoServiceImpl
 * @Description TODO()
 * @Date 2020/11/10 0010 11:52
 * @Author by Administrator
 * @Version v1.0
 */
public class UserinfoServiceImpl implements UserinfoService {

    private UserinfoDao dao = new UserinfoDaoImpl();

    @Override
    public UserinfoModel login(String username, String password, HttpServletRequest request) {
        // 校验参数
        if (null == username || null == password){
            throw new ServiceException("用户名或密码不能为空");
        }

        // 查询数据库
        UserinfoModel model = dao.selectByUsernameAndPassword(username, password);
        if (model == null){
            request.setAttribute("msg", "用户名或密码不正确");
        }else{
            // 将用户信息 放到 session
            request.getSession().setAttribute("user", model);
        }
        return model;
    }
}
